package
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import model.Robot;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Security;
	
	import global.GameState;
	import global.Message;
	import global.Resource;
	
	import view.components.ModulLoader;
	import view.components.tab.TabBar2;

	[SWF(width="780",height="550",backgroundColor="#FFFFFF")]
	public class GanDengYan extends Sprite
	{
		private var _view:View = View.getInstance();
		
		public function GanDengYan()
		{
			var tabBar:TabBar2;
		}
		
		public function setLogo(logo:DisplayObject):void
		{
			Resource.logo = logo;
		}
		
		public function setEffect(effect:Object):void
		{
			Resource.effect = effect;
		}
		
		private function setResource(resource:Object):void
		{
			Resource.setResource(resource);
		}
		
		public function setComponents(component:*):void
		{
			Resource.uicomponent = component;
			Message.famusXML = component.famous;
			Message.fastReplayXml = component.fastReplay;
		}
		
		/*public function setFastReply(xml:XML):void
		{
			Message.fastReplayXml = xml;
		}*/
		
		public function init():void
		{
			View.getInstance().stage = this.stage;
			View.getInstance().scene = this;
			
			Model.getInstance().initModels();
			View.getInstance().init();
			Control.getInstance().initControlls(); 
			
			initListeners();
			initLayers();
			
			Control.getInstance().sceneControl.switchToHall();
			
			login();
			
			loadResource();
			
			Robot.getInstance();
		}
		
		public function login():void
		{
			if (GameState.DEBUG_STATE == 1)
			{
				Model.getInstance().loginModel.loginAccess();
			}
			else
			{
				
				if (GameState.isLogin)
				{
					if (Model.getInstance().loginModel)
					{
						Model.getInstance().loginModel.onLoginResult("1");
					}
				}
			}
		}
		
		private function initListeners():void
		{
			this.addEventListener(Event.ENTER_FRAME,onFrame);
		}
		
		private function onFrame(event:Event):void
		{
			Model.getInstance().timerModel.step();
			//trace(View.getInstance().alertYesNoPanel.parent == View.getInstance().hintLayer);
		}
		
		private function initLayers():void
		{
			//addChild(View.getInstance().tableLayer);
			//addChild(_view.hallLayer);
			//addChild(_view.panelLayer);
			//addChild(_view.hintLayer);
		
			Security.loadPolicyFile(GameState.getServerLink() + "28524");
			
		}
		
		private function loadResource():void
		{
			var ml:ModulLoader = new ModulLoader(10,10);
			ml.addEventListener(Event.COMPLETE,onResourceLoadComplete);
			ml.load("resource/Resource.swf");
		}
		
		private function onResourceLoadComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onResourceLoadComplete);
			setResource(event.target.content);
			event.target.destroy();
		}
		
	}
}